<?php

Loader::using("ext::libs::date-formatter");

/**
 *
 * @export Modules
 * @export-metadata RequiredRoles:ROLE_STUDENT
 */
class InformacionLaboral extends Module {

    public function index() {
        $this->ShowInformacionLaboral();
    }

    public function ShowInformacionLaboral() {
        $student = $this->model->students->GetById($this->currentUser->getName());

        $sectores = array_flip(Sector::values());
        foreach ($sectores as &$sector) {
            $sector = str_replace("_", "/", $sector);
        }

        $this->assign("addLaboral", $this->route->action("AddInformacionLaboral"));
        $this->assign("editLaboral", $this->route->action("EditInformacionLaboral"));
        $this->assign("deleteLaboral", $this->route->action("DeleteInformacionLaboral"));

        $this->assign("historialLaboral", $student->getHistorialLaboral());
        $this->assign("student", $student);
        $this->assign("sectores", $sectores);
        $this->assign("naturalezas", array_flip(Naturaleza::values()));
        $this->assign("paises", $this->model->countries->GetAll(false));

        $this->display("ShowInformacionLaboral");
    }

    public function AddInformacionLaboral() {
        Loader::using("models::entities::HistorialLaboral");

        $InformacionLaboral = new HistorialLaboral();

        $InformacionLaboralPk = new HistorialLaboralPk();
        $InformacionLaboralPk->setCodigoInterno($this->request->post("CodigoInterno"));
        $InformacionLaboralPk->setSecuencia(0);

        $InformacionLaboral->setHistorialLaboralPk($InformacionLaboralPk);
        $this->modifyInformacionLaboral($InformacionLaboral);

        echo $this->model->employment->Create($InformacionLaboral);

        $this->ShowInformacionLaboral();
    }

    public function EditInformacionLaboral() {
        $InformacionLaboralPk = $this->retrieveHistorialLaboralPK();
        $InformacionLaboral = $this->model->employment->GetById($InformacionLaboralPk);

        $this->modifyInformacionLaboral($InformacionLaboral);

        $this->model->employment->Update($InformacionLaboral);
        $this->ShowInformacionLaboral();
    }

    public function DeleteInformacionLaboral() {
        $InformacionLaboralPk = $this->retrieveHistorialLaboralPK();
        $this->model->employment->Delete($InformacionLaboralPk);
        $this->ShowInformacionLaboral();
    }

    private function retrieveHistorialLaboralPK() {
        Loader::using("models::entities::HistorialLaboralPk");
        $InformacionLaboralPk = new HistorialLaboralPk();
        $InformacionLaboralPk->setCodigoInterno($this->request->get("CodigoInterno"));
        $InformacionLaboralPk->setSecuencia($this->request->get("Secuencia"));

        return $InformacionLaboralPk;
    }

    private function modifyInformacionLaboral(HistorialLaboral &$InformacionLaboral) {

        $InformacionLaboral->setOcupacion($this->request->post("Ocupacion"));
        $InformacionLaboral->setFechaIngreso(dateToJson($this->request->post("FechaIngreso")));
        $InformacionLaboral->setNaturaleza($this->request->post("NaturalezaEmpresa"));
        $InformacionLaboral->setSector($this->request->post("SectorEmpresa"));

        $DatosLaboral = new DatosLaboral();
        $DatosLaboral->setNombreEmpresa($this->request->post("NombreEmpresa"));

        $DatosContacto = new DatosContacto();
        $DatosContacto->setDireccion($this->request->post("Direccion"));
        $DatosContacto->setTelefono($this->request->post("Telefono"));
        $DatosContacto->getMunicipio()->setNombre($this->request->post("Ciudad"));
        $DatosLaboral->setDatosEmpresa($DatosContacto);

        $InformacionLaboral->setDatosLaboral($DatosLaboral);
        
        $InformacionLaboral->setCodigoPais($this->request->post("PaisEmpresa"));

    }

}

?>